<template>
  <div class="answer-page">
    <div class="question-detail">
      <h1 class="question-title">{{ question?.title }}</h1>
      <div class="question-content" v-if="question?.content">
        {{ question.content }}
      </div>
      <div class="question-meta">
        <div class="tags">
          <el-tag v-for="tag in question?.tags" :key="tag" size="small">{{ tag }}</el-tag>
        </div>
        <div class="metrics">
          <span class="metric">
            <el-icon><View /></el-icon>
            {{ formatNumber(question?.views || 0) }}浏览
          </span>
          <span class="metric">
            <el-icon><ChatLineRound /></el-icon>
            {{ question?.answers || 0 }}回答
          </span>
        </div>
      </div>
    </div>

    <div class="answer-editor">
      <div class="editor-header">
        <h2>写回答</h2>
        <div class="editor-tips">
          <el-alert
            title="回答注意事项"
            type="info"
            :closable="false"
            show-icon
          >
            <template #default>
              <p>1. 请确保回答对他人有帮助</p>
              <p>2. 尊重原创，避免简单复制</p>
              <p>3. 理性讨论，友善交流</p>
            </template>
          </el-alert>
        </div>
      </div>

      <div class="editor-main">
        <el-form :model="answerForm" ref="answerFormRef" :rules="rules">
          <el-form-item prop="content">
            <el-input
              v-model="answerForm.content"
              type="textarea"
              :rows="12"
              placeholder="写下你的回答..."
              resize="none"
            />
          </el-form-item>

          <el-form-item>
            <div class="editor-actions">
              <el-checkbox v-model="answerForm.anonymous">匿名回答</el-checkbox>
              <div class="action-buttons">
                <el-button @click="handleCancel">取消</el-button>
                <el-button type="primary" @click="handleSubmit" :loading="submitting">
                  发布回答
                </el-button>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useQuestionStore } from '@/stores/question'
import { View, ChatLineRound } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const questionStore = useQuestionStore()

const question = ref(null)
const submitting = ref(false)
const answerFormRef = ref(null)

const answerForm = ref({
  content: '',
  anonymous: false
})

const rules = {
  content: [
    { required: true, message: '请输入回答内容', trigger: 'blur' },
    { min: 10, message: '回答内容不能少于10个字符', trigger: 'blur' }
  ]
}

const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num
}

const loadQuestion = async () => {
  const questionId = parseInt(route.params.id)
  const allQuestions = [...questionStore.questions, ...questionStore.hotQuestions]
  question.value = allQuestions.find(q => q.id === questionId)
  
  if (!question.value) {
    ElMessage.error('问题不存在')
    router.push('/')
  }
}

const handleSubmit = async () => {
  if (!answerFormRef.value) return
  
  try {
    await answerFormRef.value.validate()
    submitting.value = true
    
    await questionStore.submitAnswer(question.value.id, {
      content: answerForm.value.content,
      anonymous: answerForm.value.anonymous
    })
    
    ElMessage.success('回答发布成功')
    router.push(`/question/${question.value.id}`)
  } catch (error) {
    console.error('Failed to submit answer:', error)
    ElMessage.error('发布失败：' + (error.message || '未知错误'))
  } finally {
    submitting.value = false
  }
}

const handleCancel = () => {
  router.back()
}

onMounted(() => {
  loadQuestion()
})
</script>

<style scoped lang="scss">
.answer-page {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.question-detail {
  background: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(18, 18, 18, 0.1);
  margin-bottom: 20px;

  .question-title {
    font-size: 24px;
    font-weight: 600;
    color: #121212;
    margin: 0 0 16px;
    line-height: 1.4;
  }

  .question-content {
    font-size: 15px;
    color: #646464;
    line-height: 1.6;
    margin-bottom: 16px;
  }

  .question-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .tags {
      display: flex;
      gap: 8px;
      flex-wrap: wrap;
    }

    .metrics {
      display: flex;
      gap: 16px;

      .metric {
        display: flex;
        align-items: center;
        gap: 4px;
        color: #8590a6;
        font-size: 14px;
      }
    }
  }
}

.answer-editor {
  background: #fff;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(18, 18, 18, 0.1);

  .editor-header {
    margin-bottom: 20px;

    h2 {
      font-size: 18px;
      font-weight: 600;
      color: #121212;
      margin: 0 0 16px;
    }
  }

  .editor-main {
    .editor-actions {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .action-buttons {
        display: flex;
        gap: 12px;
      }
    }
  }
}
</style> 